import { Menu, MenuItemConstructorOptions, BrowserWindow, app, dialog } from 'electron';

/**
 * macOS 原生菜单管理模块
 * 提供完整的原生菜单栏支持
 */

/**
 * 创建应用菜单
 */
export function createApplicationMenu(mainWindow: BrowserWindow): Menu {
  const isMac = process.platform === 'darwin';
  
  const template: MenuItemConstructorOptions[] = [
    // macOS 应用菜单
    ...(isMac ? [{
      label: app.getName(),
      submenu: [
        {
          label: `关于 ${app.getName()}`,
          role: 'about' as const
        },
        { type: 'separator' as const },
        {
          label: '偏好设置...',
          accelerator: 'Cmd+,',
          click: () => {
            // 打开设置页面
            mainWindow.webContents.send('navigate-to', '/settings');
          }
        },
        { type: 'separator' as const },
        {
          label: '服务',
          role: 'services' as const,
          submenu: []
        },
        { type: 'separator' as const },
        {
          label: `隐藏 ${app.getName()}`,
          accelerator: 'Cmd+H',
          role: 'hide' as const
        },
        {
          label: '隐藏其他',
          accelerator: 'Cmd+Alt+H',
          role: 'hideOthers' as const
        },
        {
          label: '显示全部',
          role: 'unhide' as const
        },
        { type: 'separator' as const },
        {
          label: `退出 ${app.getName()}`,
          accelerator: 'Cmd+Q',
          role: 'quit' as const
        }
      ]
    }] : []),
    
    // 文件菜单
    {
      label: '文件',
      submenu: [
        {
          label: '导入文件夹...',
          accelerator: 'Cmd+O',
          click: async () => {
            try {
              const result = await dialog.showOpenDialog(mainWindow, {
                title: '选择要导入的文件夹',
                properties: ['openDirectory', 'multiSelections'],
                message: '选择包含照片的文件夹'
              });
              
              if (!result.canceled && result.filePaths.length > 0) {
                mainWindow.webContents.send('import-folders', result.filePaths);
              }
            } catch (error) {
              console.error('打开文件夹对话框失败:', error);
            }
          }
        },
        { type: 'separator' as const },
        {
          label: '新建相册',
          accelerator: 'Cmd+N',
          click: () => {
            mainWindow.webContents.send('create-album');
          }
        },
        { type: 'separator' as const },
        ...(isMac ? [] : [
          {
            label: '退出',
            accelerator: 'Ctrl+Q',
            role: 'quit' as const
          }
        ])
      ]
    },
    
    // 编辑菜单
    {
      label: '编辑',
      submenu: [
        {
          label: '撤销',
          accelerator: 'Cmd+Z',
          role: 'undo' as const
        },
        {
          label: '重做',
          accelerator: 'Shift+Cmd+Z',
          role: 'redo' as const
        },
        { type: 'separator' as const },
        {
          label: '剪切',
          accelerator: 'Cmd+X',
          role: 'cut' as const
        },
        {
          label: '复制',
          accelerator: 'Cmd+C',
          role: 'copy' as const
        },
        {
          label: '粘贴',
          accelerator: 'Cmd+V',
          role: 'paste' as const
        },
        {
          label: '全选',
          accelerator: 'Cmd+A',
          role: 'selectAll' as const
        },
        { type: 'separator' as const },
        {
          label: '查找',
          accelerator: 'Cmd+F',
          click: () => {
            mainWindow.webContents.send('toggle-search');
          }
        }
      ]
    },
    
    // 视图菜单
    {
      label: '视图',
      submenu: [
        {
          label: '图库',
          accelerator: 'Cmd+1',
          click: () => {
            mainWindow.webContents.send('navigate-to', '/library');
          }
        },
        {
          label: '导入',
          accelerator: 'Cmd+2',
          click: () => {
            mainWindow.webContents.send('navigate-to', '/import');
          }
        },
        { type: 'separator' as const },
        {
          label: '网格视图',
          accelerator: 'Cmd+3',
          click: () => {
            mainWindow.webContents.send('set-view-mode', 'grid');
          }
        },
        {
          label: '列表视图',
          accelerator: 'Cmd+4',
          click: () => {
            mainWindow.webContents.send('set-view-mode', 'list');
          }
        },
        { type: 'separator' as const },
        {
          label: '重新载入',
          accelerator: 'Cmd+R',
          role: 'reload' as const
        },
        {
          label: '强制重新载入',
          accelerator: 'Cmd+Shift+R',
          role: 'forceReload' as const
        },
        {
          label: '切换开发者工具',
          accelerator: 'F12',
          role: 'toggleDevTools' as const
        },
        { type: 'separator' as const },
        {
          label: '实际大小',
          accelerator: 'Cmd+0',
          role: 'resetZoom' as const
        },
        {
          label: '放大',
          accelerator: 'Cmd+Plus',
          role: 'zoomIn' as const
        },
        {
          label: '缩小',
          accelerator: 'Cmd+-',
          role: 'zoomOut' as const
        },
        { type: 'separator' as const },
        {
          label: '切换全屏',
          accelerator: isMac ? 'Ctrl+Cmd+F' : 'F11',
          role: 'togglefullscreen' as const
        }
      ]
    },
    
    // 窗口菜单
    {
      label: '窗口',
      submenu: [
        {
          label: '最小化',
          accelerator: 'Cmd+M',
          role: 'minimize' as const
        },
        {
          label: '关闭',
          accelerator: 'Cmd+W',
          role: 'close' as const
        },
        ...(isMac ? [
          { type: 'separator' as const },
          {
            label: '前置全部窗口',
            role: 'front' as const
          }
        ] : [])
      ]
    },
    
    // 帮助菜单
    {
      label: '帮助',
      submenu: [
        {
          label: '学习更多',
          click: () => {
            // 可以打开帮助页面或外部链接
            mainWindow.webContents.send('show-help');
          }
        },
        {
          label: '键盘快捷键',
          accelerator: 'Cmd+/',
          click: () => {
            mainWindow.webContents.send('show-shortcuts');
          }
        },
        { type: 'separator' as const },
        {
          label: '报告问题',
          click: () => {
            // 可以打开反馈表单
            mainWindow.webContents.send('show-feedback');
          }
        }
      ]
    }
  ];
  
  return Menu.buildFromTemplate(template);
}

/**
 * 设置应用菜单
 */
export function setupApplicationMenu(mainWindow: BrowserWindow): void {
  const menu = createApplicationMenu(mainWindow);
  Menu.setApplicationMenu(menu);
  
  console.log('应用菜单设置完成');
}

/**
 * 创建上下文菜单（右键菜单）
 */
export function createContextMenu(mainWindow: BrowserWindow): Menu {
  const template: MenuItemConstructorOptions[] = [
    {
      label: '复制',
      role: 'copy'
    },
    {
      label: '粘贴',
      role: 'paste'
    },
    { type: 'separator' },
    {
      label: '全选',
      role: 'selectAll'
    },
    { type: 'separator' },
    {
      label: '检查元素',
      click: () => {
        mainWindow.webContents.inspectElement(0, 0);
      }
    }
  ];
  
  return Menu.buildFromTemplate(template);
}

/**
 * 创建 Dock 菜单 (macOS)
 */
export function createDockMenu(): Menu {
  if (process.platform !== 'darwin') {
    return Menu.buildFromTemplate([]);
  }
  
  const template: MenuItemConstructorOptions[] = [
    {
      label: '新建窗口',
      click: () => {
        // 创建新窗口的逻辑
        console.log('从 Dock 菜单创建新窗口');
      }
    },
    {
      label: '导入文件夹',
      click: () => {
        // 触发导入文件夹的逻辑
        console.log('从 Dock 菜单导入文件夹');
      }
    }
  ];
  
  return Menu.buildFromTemplate(template);
}

/**
 * 设置 Dock 菜单 (macOS)
 */
export function setupDockMenu(): void {
  if (process.platform === 'darwin') {
    const dockMenu = createDockMenu();
    app.dock.setMenu(dockMenu);
    
    console.log('Dock 菜单设置完成');
  }
}